-- PrestigeShopRender

local ClassRef = app.mvc.createListViewCell(...)

local IconRender = require("app.widget.renders.IconRender")

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.PrestigeShopRender))
    sprite:init()
    return sprite
end

function ClassRef:init(data)
    self.super.init(self, data)

    self._model = app:getInst("PrestigeShopModel")

    
    local panel = self._ui.Panel
    self:setContentSize(self._ui.Panel:getContentSize())
    panel:setVisible(false)
    
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self._ui.Panel:setVisible(true)
        self:refreshBrightStatus()
        local data = self._data
        local panelRoot = self._ui.Panel
        local nameNode = panelRoot:getChildByName("name")
        local conDesNode = panelRoot:getChildByName("Text")
        local conNumNode = panelRoot:getChildByName("num_achieve")
        if conNumNode then
            conNumNode:setVisible(false)
        end
        local goldNumNode = panelRoot:getChildByName("num_gold")
        local iconRoot = panelRoot:getChildByName("icon")

        local btnItem = panelRoot:findChild("Panel_bg/Button_bg")
        local coinIconNode = panelRoot:findChild("Panel_bg/icon_gold")

        btnItem:setZoomScale(0)
        btnItem:addClickEventListener(function()
            if self._cellModel and self._cellModel:getIndex() ~= self._cellModel:getFoucsIndex() then
                self:dispatchCellEvent("click", self._cellModel)
            end
        end)
        self:setCurrencyIcon(coinIconNode,data) 
        
        local render = IconRender.create(iconRoot)
        render:setData(data.fakeItemData or data.itemData)
        render:setTipEnable(false)
        render:setClickEnable(false)

        nameNode:setString(data.selName)
        if data.price then
            goldNumNode:setString(data.price)
        end
        goldNumNode:setVisible(data.price ~= nil)
        -- 多个道具购买
        local isMultiCost = #data.cost > 1
        for i = 1, 3 do
            panelRoot:getChildByName("icon_"..i):setVisible(false)
        end
        if isMultiCost then
            local costData = self._model:transLootToItemData(data)
            for i, item in ipairs(costData) do
                local iconNode = panelRoot:getChildByName("icon_"..i)
                local iconNumNode = iconNode:findChild("num")
                if iconNode then
                    iconNode:loadTexture(item:getIcon())
                    iconNumNode:setString(item.amount)
                    iconNode:setVisible(true)
                end
            end
        end
        local conditionStatus,conditionStr = self._model:getConditionStatusByGoods(data)
        if conditionStatus then
            if data.numType == "NoLimit" then
                conDesNode:setVisible(false)
                local condition = data.conds[1]
                if condition.condType == "Level" then --等级限制的特殊处理
                    conDesNode:setVisible(true)
                    conDesNode:setString(conditionStr)
                end
            elseif data.numType == "DayLimit" then
                conDesNode:setVisible(true)
                local conS = string.format(L("text_12200010"),data.leftNum,data.limitNum)
                conDesNode:setString(conS)
            elseif data.numType == "WeeklyLimit" then
                conDesNode:setVisible(true)
                local conS = string.format(L("text_12200011"),data.leftNum,data.limitNum)
                conDesNode:setString(conS)
            elseif data.numType == "OneLifeLimit" then
                conDesNode:setVisible(true)
                local conS = string.format(L("text_12200009"),data.leftNum)
                conDesNode:setString(conS)
            end
        else
            conDesNode:setString(conditionStr)
        end
    end
end

function ClassRef:setCurrencyIcon(iconNode, goods)
    iconNode:setVisible(false)
    local isMultiCost = self._model:isMultiCost(goods)
    if not isMultiCost then
        local itemData = UD:parseLootToBagItem(goods.cost[1])
        if itemData and itemData.getIcon then
            iconNode:setVisible(true)
            iconNode:loadTexture(itemData:getIcon())
        end
    end
end

function ClassRef:setConDesNode(str)
    self._ui.Panel:getChildByName("Text"):setString(str)
end

function ClassRef:refreshBrightStatus()
    if self._cellModel then
        self._ui["Panel/Panel_bg/Button_bg"]:setBright(self._cellModel:getIndex() ~= self._cellModel:getFoucsIndex())
    else
        self._ui["Panel/Panel_bg/Button_bg"]:setBright(true)
    end
end

function ClassRef:onFoucsIndexChange(foucsIndex)
    self:refreshBrightStatus()
end

return ClassRef